import turtle
import time

# 整数在python中用什么表示      A int  B str  C float

def write(pos, text):
    turtle.penup()
    turtle.goto(pos)
    turtle.pendown()
    turtle.write(text, False, 'center', ("FangSong", 20, "normal"))


def func_click(x, y):
    global index
    if index < len(questions):
        question = questions[index]
        if y > -100 and y < -80:
            for answer in question['answers']:
                pos = answer['pos']
                if x > pos[0]-50 and x < pos[0]+50:
                    if answer['content'] == question['right_answer']:
                        turtle.pencolor('green')
                        write([0, 0], '回答正确')
                        turtle.ontimer(draw, 2000)
                        index +=1
                    else:
                        turtle.pencolor('red')
                        write([0, 0], '回答错误')
                        time.sleep(2)
                        turtle.undo()


def draw():
    turtle.clear()
    if index< len(questions):
        question = questions[index]
        turtle.pencolor('black')
        write([0, 100], question['q'])
        for answer in question['answers']:
            write(answer['pos'], answer['content'])
    else:
        turtle.pencolor('green')
        write([0, 0], "已通关")



questions = [
    {
        "q": '整数在python中用什么表示',
         "answers": [
             {
             "pos": [-200, -100],
             "content": "A int"
             },
             {
                 "pos": [0, -100],
                 "content":"B str"
             },
             {
                 "pos": [200, -100],
                 "content": "C float"
             }
         ],
        "right_answer": "A int"
    },
{
        "q": '字符串在python中用什么表示',
         "answers": [
             {
             "pos": [-200, -100],
             "content": "A int"
             },
             {
                 "pos": [0, -100],
                 "content":"B str"
             },
             {
                 "pos": [200, -100],
                 "content": "C float"
             }
         ],
         "right_answer": "B str"
    }
]
index = 0
turtle.setup(800, 600)
turtle.bgpic("./bg.gif")
draw()
turtle.onscreenclick(func_click)
turtle.done()
